﻿using System;

namespace CSEngine
{
    public class GameBoard
    {
        private byte rows, cols;
        private SpaceColor[,] board;

        public GameBoard()
        {
            rows = 15;
            cols = 15;
            reset();
        }

        public GameBoard(byte length, byte width)
        {
            rows = length;
            cols = width;
            reset();
        }

        public static GameBoard DeepCopy(GameBoard original)
        {
            GameBoard result = new GameBoard(original.Rows,original.Cols);
            result.board = new SpaceColor[original.Rows, original.Cols];
            for (byte x = 1; x <= result.Rows; x++)
            {
                for (byte y = 1; y <= result.Cols; y++)
                {
                    HexDigit hx = (HexDigit)x;
                    HexDigit hy = (HexDigit)y;
                    result.board[x - 1, y - 1] = SpaceColor.EMPTY;
                    result.MarkSpace(hx, hy, original.GetSpace(hx, hy));
                }
            }

            return result;
        }

        public byte Rows
        {
            get { return rows; }
        }

        public byte Cols
        {
            get { return cols; }
        }

        public void reset()
        {
            board = new SpaceColor[Rows,Cols];
            for (byte x = 0; x < Rows; x++)
            {
                for (byte y = 0; y < Cols; y++)
                {
                    board[x, y] = SpaceColor.EMPTY;
                }
            }
        }

        public override string ToString()
        {
            string result = " ";
            byte x, y;
            for (x = 1; x <= Cols; x++)
                result += " " + HexDigit.FromByte(x);

            for (x = 1; x <= Rows; x++)
            {
                result += "\n" + (HexDigit)x + " ";
                for (y = 1; y <= Cols; y++)
                    result += GetSpace((HexDigit)x, (HexDigit)y).ToString() + " ";
            }

            result += '\n';

            return result;
        }
        
        public void MarkSpace(Move m, SpaceColor color)
        {
            MarkSpace(m.x, m.y, color);
        }

        public void MarkSpace(HexDigit x, HexDigit y, SpaceColor color)
        {
            board[x - 1, y - 1] = color;
        }

        public SpaceColor GetSpace(Move m)
        {
            return GetSpace(m.x, m.y);
        }

        public SpaceColor GetSpace(HexDigit x, HexDigit y)
        {
            return board[x-1, y-1];
        }
    }
}
